<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="什么是内核定时器？怎么使用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV06-08-linux定时器-01-内核定时器">
<meta property="og:url" content="https://sumumm.github.io/post/2349a0a4.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="什么是内核定时器？怎么使用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207105342816.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207104530662.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207104808736.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207105124318.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207112432711.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250208095416610.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250208101539520.png">
<meta property="article:published_time" content="2025-02-16T09:58:18.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.053Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV06-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207105342816.png">


<link rel="canonical" href="https://sumumm.github.io/post/2349a0a4.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/2349a0a4.html","path":"post/2349a0a4.html","title":"LV06-08-linux定时器-01-内核定时器"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV06-08-linux定时器-01-内核定时器 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8%EF%BC%9F"><span class="nav-text">一、内核定时器？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%86%85%E6%A0%B8%E6%97%B6%E9%97%B4%E7%AE%A1%E7%90%86"><span class="nav-text">1. 内核时间管理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%B3%BB%E7%BB%9F%E8%8A%82%E6%8B%8D%E7%8E%87"><span class="nav-text">2. 系统节拍率</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-jiffies"><span class="nav-text">3. jiffies</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-jiffies%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">3.1 jiffies是什么？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E6%BA%A2%E5%87%BA%E4%BA%86%E6%80%8E%E4%B9%88%E5%8A%9E%EF%BC%9F"><span class="nav-text">3.2 溢出了怎么办？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-jiffies%E8%BD%ACms%E3%80%81us%E3%80%81ns"><span class="nav-text">3.3 jiffies转ms、us、ns</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8%E7%AE%80%E4%BB%8B"><span class="nav-text">4. 内核定时器简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-struct-timer-list"><span class="nav-text">4.1 struct timer_list</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E7%9B%B8%E5%85%B3API"><span class="nav-text">4.2 相关API</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-DEFINE-TIMER"><span class="nav-text">4.2.1 DEFINE_TIMER()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-add-timer"><span class="nav-text">4.2.2 add_timer()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-3-del-timer"><span class="nav-text">4.2.3 del_timer()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-4-del-timer-sync"><span class="nav-text">4.2.4 del_timer_sync()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-5-mod-timer"><span class="nav-text">4.2.5 mod_timer()</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B"><span class="nav-text">4.3 简单示例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81demo%E5%AE%9E%E7%8E%B0"><span class="nav-text">二、demo实现</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AE%9A%E6%97%B6%E5%99%A8demo"><span class="nav-text">1. 定时器demo</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-demo%E6%BA%90%E7%A0%81"><span class="nav-text">1.1 demo源码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%BC%80%E5%8F%91%E6%9D%BF%E9%AA%8C%E8%AF%81"><span class="nav-text">1.2 开发板验证</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-app%E6%8E%A7%E5%88%B6%E5%AE%9A%E6%97%B6%E5%99%A8"><span class="nav-text">2. app控制定时器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-demo%E6%BA%90%E7%A0%81"><span class="nav-text">2.1 demo源码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%BC%80%E5%8F%91%E6%9D%BF%E9%AA%8C%E8%AF%81"><span class="nav-text">2.2 开发板验证</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%A7%92%E8%AE%BE%E5%A4%87"><span class="nav-text">3. 秒设备</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/2349a0a4.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV06-08-linux定时器-01-内核定时器 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV06-08-linux定时器-01-内核定时器
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-02-16 17:58:18" itemprop="dateCreated datePublished" datetime="2025-02-16T17:58:18+08:00">2025-02-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV06-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>19 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>什么是内核定时器？怎么使用？若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="3">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4" target="_blank">kernel/git/stable/linux.git - Linux kernel stable tree</a></td>        <td align="left">linux kernel源码(官网,tag 4.19.71)</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、内核定时器？"><a href="#一、内核定时器？" class="headerlink" title="一、内核定时器？"></a><font size=3>一、内核定时器？</font></h1><h2 id="1-内核时间管理"><a href="#1-内核时间管理" class="headerlink" title="1. 内核时间管理"></a><font size=3>1. 内核时间管理</font></h2><p>过 UCOS 或 FreeRTOS 的话应该知道， UCOS 或 FreeRTOS 是需要一个硬件定时器提供系统时钟，一般使用 Systick 作为系统时钟源。同理， Linux 要运行，也是需要一个系统时钟的，至于这个系统时钟是由哪个定时器提供的，就不清楚了，没有去研究过 Linux 内核这部分的逻辑。</p>
<p>但是在 CortexA7 内核中有个通用定时器，在《<a target="_blank" rel="noopener" href="https://developer.arm.com/documentation/ddi0464/latest">Cortex-A7 MPCore Technical Reference Manual r0p5</a>》的“9:Generic Timer”章节有简单的讲解，关于这个通用定时器的详细内容，可以参考《<a target="_blank" rel="noopener" href="https://developer.arm.com/documentation/ddi0406/c/">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition</a>》的“chapter B8 The Generic Timer”章节。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207105342816.png" alt="image-20250207105342816"  />

<p>这个通用定时器是可选的，猜测 Linux 会将这个通用定时器作为 Linux 系统时钟源(前提是 SOC 得选配这个通用定时器)。具体是怎么做的没有深入研究过，这里仅仅是猜测！不过对于我们 Linux 驱动编写来说，不需要深入研究这些具体的实现，只需要掌握相应的 API 函数即可，除非是内核编写者或者内核爱好者。  </p>
<h2 id="2-系统节拍率"><a href="#2-系统节拍率" class="headerlink" title="2. 系统节拍率"></a><font size=3>2. 系统节拍率</font></h2><p>Linux 内核中有大量的函数需要时间管理，比如周期性的调度程序、延时程序、对于驱动编写者来说最常用的定时器。硬件定时器提供时钟源，时钟源的频率可以设置， 设置好以后就周期性的产生定时中断，系统使用定时中断来计时。中断周期性产生的频率就是系统频率，也叫做节拍率(tick rate)(有的资料也叫系统频率)，比如 1000Hz， 100Hz 等等说的就是系统节拍率。系统节拍率是可以设置的，单位是 Hz，我们在编译 Linux 内核的时候可以通过图形化界面设置系统节拍率，我们执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207104530662.png" alt="image-20250207104530662"  />

<p>这里是4.19.91版本的内核，其他版本也一样的，按照如下路径打开配置界面：  </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Kernel Features ---&gt;</span><br><span class="line">	Timer frequency (100Hz) ---&gt;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207104808736.png" alt="image-20250207104808736"  />

<p>可以看出，可选的系统节拍率为 100Hz、 200Hz、 250Hz、 300Hz、 500Hz 和 1000Hz，默认情况下选择 100Hz。 设置好以后打开 Linux 内核源码根目录下的.config 文件，在此文件中有如下定义:</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207105124318.png" alt="image-20250207105124318"  />

<p>CONFIG_HZ 为 100， Linux 内核会使用 CONFIG_HZ 来设置自己的系统时钟。我们打开 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/asm-generic/param.h">param.h - include&#x2F;asm-generic&#x2F;param.h</a> 文件</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># <span class="keyword">undef</span> HZ</span></span><br><span class="line"><span class="meta"># <span class="keyword">define</span> HZ		CONFIG_HZ	<span class="comment">/* Internal kernel timer frequency */</span></span></span><br><span class="line"><span class="meta"># <span class="keyword">define</span> USER_HZ	100		<span class="comment">/* some user interfaces are */</span></span></span><br><span class="line"><span class="meta"># <span class="keyword">define</span> CLOCKS_PER_SEC	(USER_HZ)       <span class="comment">/* in &quot;ticks&quot; like times() */</span></span></span><br></pre></td></tr></table></figure>

<p>这里定义了一个宏 HZ，宏 HZ 就是 CONFIG_HZ，因此 HZ&#x3D;100，我们后面编写 Linux驱动的时候会常常用到 HZ，因为 HZ 表示一秒的节拍数，也就是频率。  </p>
<blockquote>
<p>系统节拍率默认为 100Hz，怎么这么小？ 100Hz 是可选的节拍率里面最小的。为什么不选择大一点的呢？这里就引出了一个问题：高节拍率和低节拍率的优缺点：  </p>
<p>（1）高节拍率会提高系统时间精度，如果采用 100Hz 的节拍率，时间精度就是 10ms，采用1000Hz 的话时间精度就是 1ms，精度提高了 10 倍。高精度时钟的好处有很多，对于那些对时间要求严格的函数来说，能够以更高的精度运行，时间测量也更加准确。</p>
<p>（2）高节拍率会导致中断的产生更加频繁，频繁的中断会加剧系统的负担， 1000Hz 和 100Hz的系统节拍率相比，系统要花费 10 倍的“精力”去处理中断。中断服务函数占用处理器的时间增加，但是现在的处理器性能都很强大，所以采用 1000Hz 的系统节拍率并不会增加太大的负载压力。根据自己的实际情况，选择合适的系统节拍率，一般我们还是采用默认的 100Hz 系统节拍率。</p>
</blockquote>
<h2 id="3-jiffies"><a href="#3-jiffies" class="headerlink" title="3. jiffies"></a><font size=3>3. jiffies</font></h2><h3 id="3-1-jiffies是什么？"><a href="#3-1-jiffies是什么？" class="headerlink" title="3.1 jiffies是什么？"></a><font size=3>3.1 jiffies是什么？</font></h3><p>Linux 内核使用全局变量 jiffies 来记录系统从启动以来的系统节拍数，系统启动的时候会将 jiffies 初始化为 0， jiffies 定义在文件  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L81">jiffies.h - include&#x2F;linux&#x2F;jiffies.h</a> （timer.h 文件中会包含这个头文件，不需要重复引用  ）中：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * The 64-bit value is not atomic - you MUST NOT read it</span></span><br><span class="line"><span class="comment"> * without sampling the sequence number in jiffies_lock.</span></span><br><span class="line"><span class="comment"> * get_jiffies_64() will do this for you as appropriate.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">extern</span> u64 __cacheline_aligned_in_smp jiffies_64;</span><br><span class="line"><span class="keyword">extern</span> <span class="type">unsigned</span> <span class="type">long</span> <span class="keyword">volatile</span> __cacheline_aligned_in_smp __jiffy_arch_data jiffies;</span><br></pre></td></tr></table></figure>

<p>第 6 行：定义了一个 64 位的 jiffies_64。  </p>
<p>第 7 行：定义了一个 unsigned long 类型的 32 位的 jiffies。  </p>
<p>jiffies_64 和 jiffies 其实是同一个东西， jiffies_64 用于 64 位系统，而 jiffies 用于 32 位系统。为了兼容不同的硬件， jiffies 其实就是 jiffies_64 的低 32 位， jiffies_64 和 jiffies 的结构如图  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250207112432711.png" alt="image-20250207112432711" style="zoom: 50%;" />

<p>当我们访问 jiffies 的时候其实访问的是 jiffies_64 的低 32 位，使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L83">get_jiffies_64()</a>这个函数可以获取 jiffies_64 的值。在 32 位的系统上读取 jiffies 的值，在 64 位的系统上 jiffes 和 jiffies_64表示同一个变量，因此也可以直接读取 jiffies 的值。所以不管是 32 位的系统还是 64 位系统，都可以使用 jiffies。  </p>
<h3 id="3-2-溢出了怎么办？"><a href="#3-2-溢出了怎么办？" class="headerlink" title="3.2 溢出了怎么办？"></a><font size=3>3.2 溢出了怎么办？</font></h3><p>前面说了 HZ 表示每秒的节拍数， jiffies 表示系统运行的 jiffies 节拍数，所以 jiffies&#x2F;HZ 就是系统运行时间，单位为秒。不管是 32 位还是 64 位的 jiffies，都有溢出的风险，溢出以后会重新从 0 开始计数，相当于绕回来了，因此有些资料也将这个现象也叫做绕回。  </p>
<p>假如 HZ 为最大值 1000 的时候， 32 位的 jiffies 只需要 49.7 天就发生了绕回，对于 64 位的 jiffies 来说大概需要5.8 亿年才能绕回，因此 jiffies_64 的绕回忽略不计。处理 32 位 jiffies 的绕回显得尤为重要 ，Linux 内核提供了下面所示的几个 API 函数来处理绕回。  </p>
<ul>
<li><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L105">time_after(unkown, known)</a></li>
<li><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L109">time_before(unkown, known)</a></li>
<li><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L111">time_after_eq(unkown, known)</a></li>
<li><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L115">time_before_eq(unkown, known)</a></li>
</ul>
<p>unkown 通常为 jiffies， known 通常是需要对比的值。如果 unkown 超过 known 的话，<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L105">time_after()</a> 函数返回真，否则返回假。如果 unkown 没有超过 known 的话  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L109">time_before()</a> 函数返回真，否则返回假。 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L111">time_after_eq()</a> 函数和 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L105">time_after()</a> 函数类似，只是多了判断等于这个条件。同理， <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L115">time_before_eq()</a> 函数和 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L109">time_before()</a> 函数也类似。比如我们要判断某段代码执行时间有没有超时，此时就可以使用如下所示代码：  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">unsigned</span> <span class="type">long</span> timeout;</span><br><span class="line">timeout = jiffies + (<span class="number">2</span> * HZ); <span class="comment">/* 超时的时间点 */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 判断有没有超时 */</span></span><br><span class="line"><span class="keyword">if</span>(time_before(jiffies, timeout)) </span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 超时未发生 */</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span> </span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 超时发生 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>timeout 就是超时时间点，比如我们要判断代码执行时间是不是超过了 2 秒，那么超时时间点就是 jiffies+(2*HZ)，如果 jiffies 大于 timeout 那就表示超时了，否则就是没有超时。第 5 行通过函数  <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L109">time_before()</a> 来判断 jiffies 是否小于 timeout，如果小于的话就表示没有超时。  </p>
<h3 id="3-3-jiffies转ms、us、ns"><a href="#3-3-jiffies转ms、us、ns" class="headerlink" title="3.3 jiffies转ms、us、ns"></a><font size=3>3.3 jiffies转ms、us、ns</font></h3><p>为了方便开发， Linux 内核提供了几个 jiffies 和 ms、 us、 ns 之间的转换函数 ：</p>
<table>
    <tr>
        <td width=360px><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L291">int jiffies_to_msecs(const unsigned long j)</a></td>
        <td rowspan="3">将 jiffies 类型的参数 j 分别转换为对应的毫秒、微秒、纳秒。</td>
    </tr>
    <tr>
        <td><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L292">int jiffies_to_usecs(const unsigned long j)</a></td>
    </tr>
    <tr>
        <td><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L294">u64 jiffies_to_nsecs(const unsigned long j)</a></td>
    </tr>
    <tr>
        <td><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L338">long msecs_to_jiffies(const unsigned int m)</a></td>
        <td rowspan="3">将毫秒、微秒、纳秒转换为 jiffies 类型。</td>
    </tr>
    <tr>
        <td><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L388">long usecs_to_jiffies(const unsigned int u)</a></td>
    </tr>
    <tr>
        <td><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/time.c#L813">unsigned long nsecs_to_jiffies(u64 n)</a></td>
    </tr>
</table>

<h2 id="4-内核定时器简介"><a href="#4-内核定时器简介" class="headerlink" title="4. 内核定时器简介"></a><font size=3>4. 内核定时器简介</font></h2><p>硬件为内核提供了一个系统定时器来计算流逝的时间（即基于未来时间点的计时方式， 以当前时刻为计时开始的起点， 以未来的某一时刻为计时的终点） ，内核只有在系统定时器的帮助下才能计算和管理时间， 但是内核定时器的精度并不高， 所以不能作为高精度定时器使用。并且</p>
<p>内核定时器的运行没有周期性， 到达计时终点后会自动关闭。 如果要实现周期性定时， 就要在定时处理函数中重新开启定时器。  </p>
<h3 id="4-1-struct-timer-list"><a href="#4-1-struct-timer-list" class="headerlink" title="4.1 struct timer_list"></a><font size=3>4.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L11">struct timer_list</a></font></h3><p>内核中 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L11">struct timer_list</a> 结构用于表示一个内核定时器：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">timer_list</span> &#123;</span></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * All fields that change during normal runtime grouped to the</span></span><br><span class="line"><span class="comment">	 * same cacheline</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">hlist_node</span>	<span class="title">entry</span>;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span>		expires; <span class="comment">/* 定时器超时时间， 单位是节拍数 */</span></span><br><span class="line">	<span class="type">void</span>			(*function)(<span class="keyword">struct</span> timer_list *);<span class="comment">/* 定时处理函数 */</span></span><br><span class="line">	u32			flags;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_LOCKDEP</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">lockdep_map</span>	<span class="title">lockdep_map</span>;</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>要使用内核定时器首先要先定义一个 timer_list 变量，表示定时器。</p>
<ul>
<li>tiemr_list 结构体的expires 成员变量表示超时时间，单位为节拍数。</li>
</ul>
<p>比如我们现在需要定义一个周期为 2 秒的定时器，那么这个定时器的超时时间就是 jiffies+(2*HZ)，因此 expires&#x3D;jiffies+(2*HZ)。 或者可以通过<a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/jiffies.h#L338">msecs_to_jiffies()</a>函数计算，expires &#x3D; jiffies+msecs_to_jiffies(2000) 也可以实现2秒的定时。</p>
<ul>
<li>function 就是定时器超时以后的定时处理函数，我们要做的工作就放到这个函数里面，需要我们编写这个定时处理函数。</li>
</ul>
<h3 id="4-2-相关API"><a href="#4-2-相关API" class="headerlink" title="4.2 相关API"></a><font size=3>4.2 相关API</font></h3><h4 id="4-2-1-DEFINE-TIMER"><a href="#4-2-1-DEFINE-TIMER" class="headerlink" title="4.2.1 DEFINE_TIMER()"></a><font size=3>4.2.1 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L72">DEFINE_TIMER()</a></font></h4><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L72">DEFINE_TIMER()</a> 宏可用于定义一个定时器结构体变量，它可以直接指定超时处理函数，但是只是定义了变量，后续还是需要调用一系列的函数对定时器进行初始化。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> __TIMER_INITIALIZER(_function, _flags) &#123;		\</span></span><br><span class="line"><span class="meta">		.entry = &#123; .next = TIMER_ENTRY_STATIC &#125;,	\</span></span><br><span class="line"><span class="meta">		.function = (_function),			\</span></span><br><span class="line"><span class="meta">		.flags = (_flags),				\</span></span><br><span class="line"><span class="meta">		__TIMER_LOCKDEP_MAP_INITIALIZER(		\</span></span><br><span class="line"><span class="meta">			__FILE__ <span class="string">&quot;:&quot;</span> __stringify(__LINE__))	\</span></span><br><span class="line"><span class="meta">	&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> DEFINE_TIMER(_name, _function)				\</span></span><br><span class="line"><span class="meta">	struct timer_list _name =				\</span></span><br><span class="line"><span class="meta">		__TIMER_INITIALIZER(_function, 0)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>_name 为定义的结构体名称， _function 为定时处理函数，可以使用以下代码对定时器和相应的定时处理函数进行定义:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">timer_handler</span><span class="params">(<span class="keyword">struct</span> timer_list *t)</span><span class="comment">//定义 function_test 定时功能函数</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//......</span></span><br><span class="line">&#125;</span><br><span class="line">DEFINE_TIMER(timer_test, timer_handler);<span class="comment">//定义一个定时器</span></span><br></pre></td></tr></table></figure>

<p>这里可以看到其实就是定义了一个全局变量，然后进行参数初始化，我们也可以自己去初始化这些成员。</p>
<h4 id="4-2-2-add-timer"><a href="#4-2-2-add-timer" class="headerlink" title="4.2.2 add_timer()"></a><font size=3>4.2.2 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1121">add_timer()</a></font></h4><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1121">add_timer()</a> 用于向 Linux 内核注册定时器，使用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1121">add_timer()</a> 函数向内核注册定时器以后，定时器就会开始运行  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">add_timer</span><span class="params">(<span class="keyword">struct</span> timer_list *timer)</span></span><br><span class="line">&#123;</span><br><span class="line">	BUG_ON(timer_pending(timer));</span><br><span class="line">	mod_timer(timer, timer-&gt;expires);</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL(add_timer);</span><br></pre></td></tr></table></figure>

<p><strong>函数参数：</strong></p>
<ul>
<li>timer：要注册的定时器。</li>
</ul>
<p><strong>返回值：</strong>无</p>
<h4 id="4-2-3-del-timer"><a href="#4-2-3-del-timer" class="headerlink" title="4.2.3 del_timer()"></a><font size=3>4.2.3 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1181">del_timer()</a></font></h4><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1181">del_timer()</a> 函数用于删除一个定时器，不管定时器有没有被激活，都可以使用此函数删除。在多处理器系统上，定时器可能会在其他的处理器上运行，因此在调用 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1181">del_timer()</a> 函数删除定时器之前要先等待其他处理器的定时处理器函数退出。   </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">del_timer</span><span class="params">(<span class="keyword">struct</span> timer_list *timer)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">timer_base</span> *<span class="title">base</span>;</span></span><br><span class="line">	<span class="type">unsigned</span> <span class="type">long</span> flags;</span><br><span class="line">	<span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	debug_assert_init(timer);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (timer_pending(timer)) &#123;</span><br><span class="line">		base = lock_timer_base(timer, &amp;flags);</span><br><span class="line">		ret = detach_if_pending(timer, base, <span class="literal">true</span>);</span><br><span class="line">		raw_spin_unlock_irqrestore(&amp;base-&gt;lock, flags);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL(del_timer);</span><br></pre></td></tr></table></figure>

<p><strong>函数参数：</strong></p>
<ul>
<li>timer：要删除的定时器。</li>
</ul>
<p><strong>返回值：</strong>0，定时器还没被激活； 1，定时器已经激活。  </p>
<h4 id="4-2-4-del-timer-sync"><a href="#4-2-4-del-timer-sync" class="headerlink" title="4.2.4 del_timer_sync()"></a><font size=3>4.2.4 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L175">del_timer_sync()</a></font></h4><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L175">del_timer_sync()</a> 函数是 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1181">del_timer()</a> 函数的同步版，会等待其他处理器使用完定时器再删除， <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/include/linux/timer.h#L175">del_timer_sync()</a> 不能使用在中断上下文中。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifdef</span> CONFIG_SMP</span></span><br><span class="line">  <span class="keyword">extern</span> <span class="type">int</span> <span class="title function_">del_timer_sync</span><span class="params">(<span class="keyword">struct</span> timer_list *timer)</span>;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line"><span class="meta"># <span class="keyword">define</span> del_timer_sync(t)		del_timer(t)</span></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>

<p><strong>函数参数：</strong></p>
<ul>
<li>timer：要删除的定时器。</li>
</ul>
<p><strong>返回值：</strong>0，定时器还没被激活； 1，定时器已经激活。  </p>
<h4 id="4-2-5-mod-timer"><a href="#4-2-5-mod-timer" class="headerlink" title="4.2.5 mod_timer()"></a><font size=3>4.2.5 <a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1080">mod_timer()</a></font></h4><p><a target="_blank" rel="noopener" href="https://elixir.bootlin.com/linux/v4.19.71/source/kernel/time/timer.c#L1080">mod_timer()</a> 函数用于修改定时值，如果定时器还没有激活的话， mod_timer 函数会激活定时器  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">mod_timer</span><span class="params">(<span class="keyword">struct</span> timer_list *timer, <span class="type">unsigned</span> <span class="type">long</span> expires)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">return</span> __mod_timer(timer, expires, <span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line">EXPORT_SYMBOL(mod_timer);</span><br></pre></td></tr></table></figure>

<p><strong>函数参数：</strong></p>
<ul>
<li>timer：要修改超时时间(定时值)的定时器。  </li>
<li>expires：修改后的超时时间。</li>
</ul>
<p><strong>返回值：</strong>0，调用 mod_timer 函数前定时器未被激活； 1，调用 mod_timer 函数前定时器已被激活。  </p>
<blockquote>
<p>Tips：</p>
<p>若是最开始定时器的超时时间为5s，现在在第1s，这个时候修改了定时器的超时时间为2s，此时，定时器会根据当前时间（第1秒）重新计算触发时间。新的触发时间为当前时间（第1秒）加上新的超时时间（2秒），即第3秒。所以定时器将在第4秒时触发，执行预设的回调函数。</p>
</blockquote>
<h3 id="4-3-简单示例"><a href="#4-3-简单示例" class="headerlink" title="4.3 简单示例"></a><font size=3>4.3 简单示例</font></h3><p>内核定时器一般的使用流程如下所示：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">timer_handler</span><span class="params">(<span class="keyword">struct</span> timer_list *t)</span>;</span><br><span class="line">DEFINE_TIMER(timer_test, timer_handler);<span class="comment">//定义一个定时器变量并指定超时处理函数</span></span><br><span class="line"></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> <span class="title function_">timer_handler</span><span class="params">(<span class="keyword">struct</span> timer_list *t)</span><span class="comment">//定义 function_test 定时功能函数</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 定时器处理代码 */</span></span><br><span class="line">    <span class="comment">/* 如果需要定时器周期性运行的话就使用 mod_timer 函数重新设置超时值并且启动定时器。*/</span></span><br><span class="line">    mod_timer(&amp;timer_test, jiffies + msecs_to_jiffies(<span class="number">2000</span>));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 初始化/打开一个定时器 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">timer_init</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    timer_test.expires = jffies + msecs_to_jiffies(<span class="number">2000</span>); <span class="comment">/* 超时时间 2 秒 */</span></span><br><span class="line">    add_timer(&amp;timer_test); <span class="comment">/* 启动定时器 */</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 关闭/删除定时器 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">timer_destory</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    del_timer(&amp;timer_test); <span class="comment">/* 删除定时器 */</span></span><br><span class="line">    <span class="comment">/* 或者使用 */</span></span><br><span class="line">    del_timer_sync(&amp;timer_test);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="二、demo实现"><a href="#二、demo实现" class="headerlink" title="二、demo实现"></a><font size=3>二、demo实现</font></h1><h2 id="1-定时器demo"><a href="#1-定时器demo" class="headerlink" title="1. 定时器demo"></a><font size=3>1. 定时器demo</font></h2><h3 id="1-1-demo源码"><a href="#1-1-demo源码" class="headerlink" title="1.1 demo源码"></a><font size=3>1.1 demo源码</font></h3><p><a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-driver-demo/tree/master/09_kernel_timer/01_timer_basic">09_kernel_timer&#x2F;01_timer_basic · 苏木&#x2F;imx6ull-driver-demo - 码云 - 开源中国</a></p>
<h3 id="1-2-开发板验证"><a href="#1-2-开发板验证" class="headerlink" title="1.2 开发板验证"></a><font size=3>1.2 开发板验证</font></h3><p>这个基本的定时器测试demo就是在驱动中直接打开的，我们使用以下命令加载驱动就会启动定时器：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">insmod sdriver_demo.ko</span><br></pre></td></tr></table></figure>

<p>然后会看到以下打印信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250208095416610.png" alt="image-20250208095416610"  />

<p>可以看到超时时间是5s，基本上看内核日志的时间戳就可以知道每次打印间隔就是5s。</p>
<h2 id="2-app控制定时器"><a href="#2-app控制定时器" class="headerlink" title="2. app控制定时器"></a><font size=3>2. app控制定时器</font></h2><p>app怎么控制定时器？我们可以通过ioctl方法来实现，我们可以定义三个命令：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> CMD_TIMER_SETPERIOD   (_IOW(<span class="string">&#x27;T&#x27;</span>, 100, int)) <span class="comment">/* 设置定时器周期命令 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CMD_TIMER_OPEN        (_IO(<span class="string">&#x27;T&#x27;</span>, 101))       <span class="comment">/* 打开内核定时器 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CMD_TIMER_CLOSE       (_IO(<span class="string">&#x27;T&#x27;</span>, 102))       <span class="comment">/* 关闭内核定时器 */</span></span></span><br></pre></td></tr></table></figure>

<h3 id="2-1-demo源码"><a href="#2-1-demo源码" class="headerlink" title="2.1 demo源码"></a><font size=3>2.1 demo源码</font></h3><p><a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-driver-demo/tree/master/09_kernel_timer/02_timer_ctl">09_kernel_timer&#x2F;02_timer_ctl · 苏木&#x2F;imx6ull-driver-demo - 码云 - 开源中国</a></p>
<h3 id="2-2-开发板验证"><a href="#2-2-开发板验证" class="headerlink" title="2.2 开发板验证"></a><font size=3>2.2 开发板验证</font></h3><p>我们执行以下命令，来验证效果：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./app_demo.out /dev/sdevchr 4</span><br></pre></td></tr></table></figure>

<p>然后们可以看到以下打印信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV06-08-linux%E5%AE%9A%E6%97%B6%E5%99%A8-01-%E5%86%85%E6%A0%B8%E5%AE%9A%E6%97%B6%E5%99%A8/img/image-20250208101539520.png" alt="image-20250208101539520"  />

<p>可以看到打开定时器后，默认超时时间为5s，之后改为2秒超时，每次打印间隔变成了2s。</p>
<h2 id="3-秒设备"><a href="#3-秒设备" class="headerlink" title="3. 秒设备"></a><font size=3>3. 秒设备</font></h2><p>我们来通过定时器定义一个秒设备。这个直接看demo吧：</p>
<p><a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-driver-demo/tree/master/09_kernel_timer/03_timer_dev">09_kernel_timer&#x2F;03_timer_dev · 苏木&#x2F;imx6ull-driver-demo - 码云 - 开源中国</a></p>
<p>其实就是定义一个变量，再定义一个定时器，每秒让变量自增，应用再来读取。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/2349a0a4.html">LV06-08-linux定时器-01-内核定时器</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2025年02月16日 - 17:58</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/2349a0a4.html" title="LV06-08-linux定时器-01-内核定时器">https://sumumm.github.io/post/2349a0a4.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV06-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV06-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/d26cc426.html" rel="prev" title="LV05-02-U-Boot-06-02-menuconfig">
                  <i class="fa fa-angle-left"></i> LV05-02-U-Boot-06-02-menuconfig
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/431d1efd.html" rel="next" title="LV06-07-IO模型-05-多路复用IO">
                  LV06-07-IO模型-05-多路复用IO <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
